package com.qfedu.entity.course05;

public class MyHashTable {
    // 创建哈希表
    private EmpLinkedList[] empLinkedListArray;
    private int size;

    public MyHashTable(int size) {
        this.size = size;
        empLinkedListArray = new EmpLinkedList[size];  // 初始化哈希表
        for (int i = 0; i < size; i++) {
            empLinkedListArray[i] = new EmpLinkedList();  //初始化哈希表中每一个链表
        }
    }

    public static void main(String[] args) {
        MyHashTable hashtable = new MyHashTable(7);
        Emp emp1 = new Emp(1, "张三");
        Emp emp2 = new Emp(2, "李四");
        Emp emp3 = new Emp(8, "王五");
        hashtable.add(emp1);
        hashtable.add(emp2);
        hashtable.add(emp3);
        hashtable.getList();
        hashtable.delete(1);
        hashtable.getList();
    }

    // 新增雇员
    public void add(Emp emp) {
        int i = hashFun(emp.id);
        empLinkedListArray[i].add(emp);
    }

    // 遍历雇员
    public void getList() {
        for (int i = 0; i < size; i++) {
            empLinkedListArray[i].getList();
        }
    }

    // 根据id查找雇员
    public void get(int id) {
        int i = hashFun(id);
        Emp emp = empLinkedListArray[i].get(id);
        if (emp == null) {
            System.out.println("未找到该雇员");
        } else {
            System.out.println(emp.id + ":" + emp.name);
        }
    }

    //删除雇员
    public void delete(int id) {
        int i = hashFun(id);
        empLinkedListArray[i].delete(id);
    }

    //编写散列函数，取模
    private int hashFun(int id) {
        return id % size;
    }

}


// 创建雇员表
class Emp {
    public int id;
    public String name;
    public Emp next;

    public Emp(int id, String name) {
        this.id = id;
        this.name = name;
    }
}

//创建一个链表，存放雇员
class EmpLinkedList {
    // 创建一个无头指针的链表，第一个节点就为第一个雇员,默认为null
    private Emp head;

    //添加元素
    public void add(Emp emp) {
        // 如果链表为空，直接插入
        if (head == null) {
            head = emp;
            return;
        }
        Emp temp = head;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.next = emp;
    }

    // 遍历雇员
    public void getList() {
        // 判断链表是否为空
        if (head == null) {
            System.out.println("没有雇员");
            return;
        }
        Emp temp = head;
        while (temp != null) {
            System.out.println(temp.id + ":" + temp.name);
            temp = temp.next;
        }
    }

    //根据id查找雇员
    public Emp get(int id) {
        if (head == null) {
            System.out.println("没有雇员");
            return null;
        }
        Emp temp = head;
        while (temp != null) {
            if (temp.id == id) {
                return temp;  // 找到后返回
            }
            temp = temp.next;
        }
        return null;
    }

    // 删除雇员
    public void delete(int id) {
        if (head == null) {
            System.out.println("没有雇员");
            return;
        }
        Emp temp = head;
        while (true) {
            if (temp.id == id) {
                head = temp.next;
                return;
            }
            if (temp.next.id == id) {
                temp.next = temp.next.next;
                return;
            }
            if (temp.next == null) {
                System.out.println("未找到");
                return;
            }
            temp = temp.next;
        }
    }
}

